home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / iccfont.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  265 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* iccfont.c */
  20. /* Initialization support for compiled fonts */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "ccfont.h"
  25. #include "dict.h"
  26. #include "dstack.h"
  27. #include "errors.h"
  28. #include "font.h"
  29. #include "iname.h"
  30. #include "iutil.h"
  31. #include "oper.h"
  32. #include "save.h"            /* for alloc_refs */
  33. #include "store.h"
  34.  
  35. /* Forward references */
  36. private int huge
  37.     cfont_ref_dict_create(P4(ref *, const cfont_dict_keys *,
  38.                  cfont_string_array, const ref *)),
  39.     cfont_string_dict_create(P4(ref *, const cfont_dict_keys *,
  40.                     cfont_string_array, cfont_string_array)),
  41.     cfont_num_dict_create(P4(ref *, const cfont_dict_keys *,
  42.                  cfont_string_array, const float *)),
  43.     cfont_name_array_create(P3(ref *, cfont_string_array, int)),
  44.     cfont_string_array_create(P4(ref *, cfont_string_array, int, uint)),
  45.     cfont_name_create(P2(ref *, const char *));
  46.  
  47. /* Procedure vector passed to font initialization procedures. */
  48. private const cfont_procs ccfont_procs = {
  49.     cfont_ref_dict_create,
  50.     cfont_string_dict_create,
  51.     cfont_num_dict_create,
  52.     cfont_name_array_create,
  53.     cfont_string_array_create,
  54.     cfont_name_create
  55. };
  56.  
  57. /* ------ Initialization tables ------ */
  58.  
  59. /*
  60.  * The file gconfigf.h contains a line
  61.  *    font_("0.font_xxx", gsf_xxx, zf_xxx)
  62.  * for each compiled font.
  63.  */
  64.  
  65. /* Generate an operator procedure for each font. */
  66. #ifdef __PROTOTYPES__
  67. #  define zfproto(proc) proc(os_ptr op)
  68. #else
  69. #  define zfproto(proc) proc(op) os_ptr op;
  70. #endif
  71. #define font_(fname, fproc, zfproc)\
  72. extern int huge fproc(P2(const cfont_procs *, ref *));\
  73. private int zfproto(zfproc)\
  74. {    int code = fproc(&ccfont_procs, (ref *)(op + 1));\
  75.     if ( code < 0 ) return code;\
  76.     push(1);\
  77.     return 0;\
  78. }
  79. #include "gconfigf.h"
  80.  
  81. /* Generate the operator initialization table. */
  82. #undef font_
  83. #define font_(fname, fproc, zfproc)\
  84.     {fname, zfproc},
  85. op_def ccfonts_op_defs[] = {
  86. #include "gconfigf.h"
  87.     op_def_end(0)
  88. };
  89.  
  90. /* ------ Procedural code ------ */
  91.  
  92. typedef struct {
  93.     const char *str_array;
  94.     const byte *str;
  95.     uint len;
  96. } str_enum;
  97. #define init_str_enum(sep, ksa)\
  98.   (sep)->str_array = ksa
  99. typedef struct {
  100.     cfont_dict_keys keys;
  101.     str_enum strings;
  102. } key_enum;
  103. #define init_key_enum(kep, kp, ksa)\
  104.   (kep)->keys = *kp, init_str_enum(&(kep)->strings, ksa)
  105.  
  106. /* Check for reaching the end of the keys. */
  107. #define more_keys(kep) ((kep)->keys.num_enc_keys | (kep)->keys.num_str_keys)
  108.  
  109. /* Get the next string from a string array. */
  110. /* Return true if it was a string, false if it was a null. */
  111. private int near
  112. cfont_next_string(str_enum _ss *pse)
  113. {    const char *str = pse->str_array;
  114.     uint len = (((const byte *)str)[0] << 8) + ((const byte *)str)[1];
  115.     int was_string = 1;
  116.     if ( len == 0xffff )
  117.     {    len = 0;
  118.         was_string = 0;
  119.     }
  120.     pse->str = (const byte *)str + 2;
  121.     pse->len = len;
  122.     pse->str_array = str + 2 + len;
  123.     return was_string;
  124. }
  125.  
  126. /* Put the next entry into a dictionary. */
  127. /* We know that more_keys(kp) is true. */
  128. private int near
  129. cfont_put_next(ref *pdict, key_enum _ss *kep, const ref *pvalue)
  130. {    ref kname;
  131.     int code;
  132. #define kp (&kep->keys)
  133.     if ( pdict->value.pdict == 0 )
  134.     {    /* First time, create the dictionary. */
  135.         code = dict_create(kp->num_enc_keys + kp->num_str_keys + kp->extra_slots, pdict);
  136.         if ( code < 0 )
  137.             return code;
  138.     }
  139.     if ( kp->num_enc_keys )
  140.     {    const charindex *skp = kp->enc_keys++;
  141.         code = array_get(®istered_Encodings[skp->encx], (long)(skp->charx), &kname);
  142.         kp->num_enc_keys--;
  143.     }
  144.     else        /* must have kp->num_str_keys != 0 */
  145.     {    cfont_next_string(&kep->strings);
  146.         code = name_ref(kep->strings.str, kep->strings.len, &kname, 0);
  147.         kp->num_str_keys--;
  148.     }
  149.     if ( code < 0 )
  150.         return code;
  151.     return dict_put(pdict, &kname, pvalue);
  152. #undef kp
  153. }
  154.  
  155. /* ------ Routines called from compiled font initialization ------ */
  156.  
  157. /* Create a dictionary with general ref values. */
  158. private int huge
  159. cfont_ref_dict_create(ref *pdict, const cfont_dict_keys *kp,
  160.   cfont_string_array ksa, const ref *values)
  161. {    key_enum kenum;
  162.     const ref *vp = values;
  163.     init_key_enum(&kenum, kp, ksa);
  164.     pdict->value.pdict = 0;
  165.     while ( more_keys(&kenum) )
  166.     {    const ref *pvalue = vp++;
  167.         int code = cfont_put_next(pdict, &kenum, pvalue);
  168.         if ( code < 0 ) return code;
  169.     }
  170.     return 0;
  171. }
  172.  
  173. /* Create a dictionary with string/null values. */
  174. private int huge
  175. cfont_string_dict_create(ref *pdict, const cfont_dict_keys *kp,
  176.   cfont_string_array ksa, cfont_string_array kva)
  177. {    key_enum kenum;
  178.     str_enum senum;
  179.     uint attrs = kp->value_attrs;
  180.     init_key_enum(&kenum, kp, ksa);
  181.     init_str_enum(&senum, kva);
  182.     pdict->value.pdict = 0;
  183.     while ( more_keys(&kenum) )
  184.     {    ref vstring;
  185.         int code;
  186.         if ( cfont_next_string(&senum) )
  187.         {    make_const_string(&vstring, attrs, senum.len,
  188.                       (const byte *)senum.str);
  189.         }
  190.         else
  191.             make_null(&vstring);
  192.         code = cfont_put_next(pdict, &kenum, &vstring);
  193.         if ( code < 0 ) return code;
  194.     }
  195.     return 0;
  196. }
  197.  
  198. /* Create a dictionary with number values. */
  199. private int huge
  200. cfont_num_dict_create(ref *pdict, const cfont_dict_keys *kp,
  201.   cfont_string_array ksa, const float *values)
  202. {    key_enum kenum;
  203.     const float *vp = values;
  204.     ref vnum;
  205.     init_key_enum(&kenum, kp, ksa);
  206.     pdict->value.pdict = 0;
  207.     while ( more_keys(&kenum) )
  208.     {    float val = *vp++;
  209.         int code;
  210.         if ( val == (int)val )
  211.             make_int(&vnum, (int)val);
  212.         else
  213.             make_real(&vnum, val);
  214.         code = cfont_put_next(pdict, &kenum, &vnum);
  215.         if ( code < 0 ) return code;
  216.     }
  217.     return 0;
  218. }
  219.  
  220. /* Create an array with name values. */
  221. private int huge
  222. cfont_name_array_create(ref *parray, cfont_string_array ksa, int size)
  223. {    int code = alloc_array(parray, a_readonly, size, "cfont_name_array_create");
  224.     ref *aptr = parray->value.refs;
  225.     int i;
  226.     str_enum senum;
  227.     if ( code < 0 ) return code;
  228.     init_str_enum(&senum, ksa);
  229.     for ( i = 0; i < size; i++, aptr++ )
  230.     {    ref nref;
  231.         cfont_next_string(&senum);
  232.         code = name_ref((const byte *)senum.str, senum.len, &nref, 0);
  233.         if ( code < 0 ) return code;
  234.         ref_assign_new(aptr, &nref);
  235.     }
  236.     return 0;
  237. }
  238.  
  239. /* Create an array with string/null values. */
  240. private int huge
  241. cfont_string_array_create(ref *parray, cfont_string_array ksa,
  242.   int size, uint attrs)
  243. {    int code = alloc_array(parray, a_readonly, size, "cfont_string_array_create");
  244.     ref *aptr = parray->value.refs;
  245.     int i;
  246.     str_enum senum;
  247.     if ( code < 0 ) return code;
  248.     init_str_enum(&senum, ksa);
  249.     for ( i = 0; i < size; i++, aptr++ )
  250.     {    if ( cfont_next_string(&senum) )
  251.         {    make_tasv_new(aptr, t_string, attrs, senum.len,
  252.                       const_bytes, (const byte *)senum.str);
  253.         }
  254.         else
  255.             make_null_new(aptr);
  256.     }
  257.     return 0;
  258. }
  259.  
  260. /* Create a name. */
  261. private int huge
  262. cfont_name_create(ref *pnref, const char *str)
  263. {    return name_ref((const byte *)str, strlen(str), pnref, 0);
  264. }
  265.